﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lambda
{
   public class LambdaShow
   {

       public delegate void NoReturnNoPara();

       public delegate int WithReturnNoPara();

       public delegate void NoReturnWithPara(int id, string name);

       public delegate LambdaShow WithReturnWithPara(DateTime time);



       public void Show()
       {
           string nameOut = "糯米飞机";
           {
               Student student = new Student();
               student.Id = 1;
               student.Name = "打兔子的猎人";
               student.Age = 27;
               student.ClassId = 2;

               student.Study();
           }
           {
               Student student = new Student()
               {
                   Id = 1,
                   Name = "打兔子的猎人",
                   Age = 27,
                   ClassId = 2
               };
               student.Study();
           }

           {
               NoReturnWithPara method = new NoReturnWithPara(this.DoNothing);//2 委托的实例化
               method.Invoke(1, "Gain");//3 委托的调用
           }

           {
               NoReturnWithPara method=DoNothing;
               method.Invoke(2, "憨豆");
           }

           {
               NoReturnWithPara method = new NoReturnWithPara
               (
                   delegate(int id, string name)
                   {
                       Console.WriteLine(nameOut);
                       Console.WriteLine("{0} {1} DoNothing");
                   }
               );
           }

           {
               NoReturnWithPara method = new NoReturnWithPara
                   (
                   (int id, string name) =>
                   {
                       Console.WriteLine(nameOut);
                       Console.WriteLine("{0} {1} DoNothing");
                   }
                
                   );
           }
           //如果方法体只有一行，那么可以去掉大括号和分号,多行不行
           //如果参数只有一个  可以去掉小括号
           //如果没有参数，直接用小括号
            {
                NoReturnWithPara method = (id,name) => Console.WriteLine("{0} {1} DoNothing");
            }

            //无返回值的委托
            Action action=() => Console.WriteLine("{0} {1} DoNothing");
            Action<int> action1 = t => {
                if (t==2)
                {
                    Console.WriteLine("{0} {1} DoNothing");
                }
            };
            Action<int,string> action3 = (t, m)=>{ };
            Action<int, NoReturnWithPara, LambdaShow, LambdaShow, string, string, string, string, string, string, string, string, string, string, string, string> act4 = null;
            Action<int, string, string, string, string, string, string, string, string, string, string, string, string> act5 = null;

            //带返回值的委托
            Func<int> func = () => 1;
            int i = func();

            Func<int, string> func2 = t=> "char";
            string a = func2(1);

            Func<int,int, string> func3 = (t,k) => "char";

            Func<int, int, NoReturnWithPara, LambdaShow, LambdaShow, string, string, string, string, string, string, string, string, string, string, string, string> func4 = null;

            {
                //多返回值：1 返回对象  2  ref out
                WithReturnWithParaRefOut method = new WithReturnWithParaRefOut(
                    delegate (ref DateTime time, out int o)
                    {
                        o = 1;
                        return this;
                    });//lambda表达式不行
            }
       }


       public delegate LambdaShow WithReturnWithParaRefOut(ref DateTime time, out int i);

       private void DoNothing(int id, string name)
       {
           Console.WriteLine("{0} {1} DoNothing out");
       }

       private void DoNothing1()
       {
           Console.WriteLine("DoNothing");
       }

       private void DoNothing2()
       {
           Console.WriteLine("DoNothing");
       }


    }


    }

